package appConfig

import (
	"errors"
	"fmt"
	"strconv"
	"strings"
	"sync"
)

var faker_local_request_domain = ".transport.playnas.me"
var faker_remote_request_domain = ".remote.playnas.me"
var web_gateway = "172.16.0."

type HostConf struct {
	HostMap    sync.Map
	ReverseMap sync.Map
	used       map[string]string
	usedInt    int
	locker     sync.Mutex
}

func (conf *HostConf) SetConf(Host string, ip string) {
	conf.HostMap.Store(ip, Host)
	conf.ReverseMap.Store(Host, ip)
}

func (conf *HostConf) GetHost(ip string) (string, error) {
	if value, ok := conf.HostMap.Load(ip); ok {
		return value.(string), nil
	}
	return "", errors.New("找不到对应的HOST地址")
}
func (conf *HostConf) GetIp(host string) (string, error) {
	if value, ok := conf.ReverseMap.Load(host); ok {
		return value.(string), nil
	}
	return "", errors.New("找不到对应的IP地址")
}
func (conf *HostConf) ResolveSidByHost(host string) string {
	sidList := strings.Split(host, ".")
	return sidList[0]
}

func (conf *HostConf) CreateRequestUserHost(sid string) (string, error) {
	host := sid + faker_remote_request_domain
	conf.SetConf(host, "172.16.0.13")
	return conf.GetIp(host)
}

func (conf *HostConf) CreateRemoteRequestUserHost(sid string) (string, error) {
	host := sid + faker_remote_request_domain
	ip, err := conf.SetRequestIp(sid)
	if err != nil {
		return "", nil
	}
	conf.SetConf(host, ip)
	return conf.GetIp(host)
}

func (conf *HostConf) CreateLocalRequestUserHostForce(sid string) string {
	conf.locker.Lock()
	defer conf.locker.Unlock()
	conf.used[sid] = web_gateway + "11"
	return web_gateway + "11"

}

func (conf *HostConf) CreateLocalRequestUserHost(sid string) (string, error) {
	host := sid + faker_local_request_domain
	ip, err := conf.SetRequestIp(sid)
	if err != nil {
		return "", nil
	}
	conf.SetConf(host, ip)
	return conf.GetIp(host)
}

func (conf *HostConf) GetLocalRequestIp(sid string) (string, error) {
	host := sid + faker_local_request_domain
	return conf.GetIp(host)
}

func (conf *HostConf) CheckIsLocalRequestIp(host string) bool {
	return strings.Index(host, faker_local_request_domain) != -1
}

func contains(s []string, elem string) bool {
	for _, a := range s {
		if a == elem {
			return true
		}
	}
	return false
}

// SetRequestIp 分配访问的ip地址
func (conf *HostConf) SetRequestIp(targetId string) (string, error) {
	if ip, ok := conf.used[targetId]; ok {
		return ip, nil
	}
	conf.locker.Lock()
	defer conf.locker.Unlock()
	conf.usedInt += conf.usedInt
	allList := make([]string, 0)
	for _, value := range conf.used {
		allList = append(allList, value)
	}
	i := 11
	for i <= 255 {
		if !contains(allList, web_gateway+strconv.Itoa(i)) {
			conf.used[targetId] = web_gateway + strconv.Itoa(i)
			return web_gateway + strconv.Itoa(i), nil
		}
		i++
	}
	return "", errors.New("分配局域网失败")
}

var HostConfManager *HostConf

func init() {
	HostConfManager = &HostConf{
		usedInt: 11,
		used:    map[string]string{},
	}
	HostConfManager.CreateLocalRequestUserHostForce(GetLocalSid())
	HostConfManager.CreateLocalRequestUserHost(GetLocalSid())
	fmt.Println("------------->")
	//HostConfManager.SetConf("526c98f81bcf760d95bb24e6facb7ac1"+faker_local_request_domain, "172.16.0.13")
	//HostConfManager.SetConf("526c98f81bcf760d95bb24e6facb7ac1"+faker_remote_request_domain, "172.16.0.13")
	//HostConfManager.SetConf("726c98f81bcf760d95bb24e6facb7ac1"+faker_local_request_domain, "172.16.0.14"  )
	//HostConfManager.SetConf("726c98f81bcf760d95bb24e6facb7ac1"+faker_remote_request_domain, "172.16.0.14")
	//HostConfManager.SetConf("826c98f81bcf760d95bb24e6facb7ac1"+faker_local_request_domain, "172.16.0.15")
	//HostConfManager.SetConf("826c98f81bcf760d95bb24e6facb7ac1"+faker_remote_request_domain, "172.16.0.13")
}
